<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教师姓名" prop="scgTeacherName">
        <el-input
          v-model="queryParams.scgTeacherName"
          placeholder="请输入教师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="竞赛名称" prop="scgCompetitionName">
        <el-input
          v-model="queryParams.scgCompetitionName"
          placeholder="请输入竞赛名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作品名称" prop="scgWinningWorkName">
        <el-input
          v-model="queryParams.scgWinningWorkName"
          placeholder="请输入作品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="竞赛级别" prop="scgAwardLevel">
        <el-select v-model="queryParams.scgAwardLevel" placeholder="请选择竞赛级别" clearable>
          <el-option
            v-for="dict in dict.type.rp_employee_number"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="获奖等级" prop="scgAwardGrade">
        <el-select v-model="queryParams.scgAwardGrade" placeholder="请选择获奖等级" clearable>
          <el-option
            v-for="dict in dict.type.scg_award_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学生姓名1" prop="scgStudentName1">
        <el-input
          v-model="queryParams.scgStudentName1"
          placeholder="请输入学生姓名1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名2" prop="scgStudentName2">
        <el-input
          v-model="queryParams.scgStudentName2"
          placeholder="请输入学生姓名2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名3" prop="scgStudentName3">
        <el-input
          v-model="queryParams.scgStudentName3"
          placeholder="请输入学生姓名3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名4" prop="scgStudentName4">
        <el-input
          v-model="queryParams.scgStudentName4"
          placeholder="请输入学生姓名4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名5" prop="scgStudentName5">
        <el-input
          v-model="queryParams.scgStudentName5"
          placeholder="请输入学生姓名5"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名6" prop="scgStudentName6">
        <el-input
          v-model="queryParams.scgStudentName6"
          placeholder="请输入学生姓名6"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授予部门" prop="scgAwardingDepartment">
        <el-input
          v-model="queryParams.scgAwardingDepartment"
          placeholder="请输入授予部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授予时间">
        <el-date-picker
          v-model="daterangeScgAwardTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="竞赛类型" prop="scgCompetitionType">
        <el-select v-model="queryParams.scgCompetitionType" placeholder="请选择竞赛类型" clearable>
          <el-option
            v-for="dict in dict.type.ta_award_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['guidance:guidance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['guidance:guidance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['guidance:guidance:remove']"
        >删除</el-button>
      </el-col>
      <!-- 审核   -->
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-check"
          size="mini"
          @click="openAuditDialog"
          :disabled="multiple"
          v-hasPermi="['system:check']"
          plain
        >审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['guidance:guidance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 确保visible是通过sync双向绑定 -->
    <audit-dialog
      :visible.sync="auditDialogVisible"
      table_name="jw_stu_competition_guidance"
      id_name="scg_id"
      ref="audit"
      @submit="handleAuditSubmit"
    />

    <el-table v-loading="loading" :data="guidanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="scgId" />
      <el-table-column label="教师姓名" align="center" prop="scgTeacherName" />
      <el-table-column label="教工号" align="center" prop="scgEmployeeNumber" />
      <el-table-column label="竞赛名称" align="center" prop="scgCompetitionName" />
      <el-table-column label="作品名称" align="center" prop="scgWinningWorkName" />
      <el-table-column label="竞赛级别" align="center" prop="scgAwardLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rp_employee_number" :value="scope.row.scgAwardLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="获奖等级" align="center" prop="scgAwardGrade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.scg_award_grade" :value="scope.row.scgAwardGrade"/>
        </template>
      </el-table-column>
      <el-table-column label="学生姓名1" align="center" prop="scgStudentName1" />
      <el-table-column label="学生学号1" align="center" prop="scgStudentId1" />
      <el-table-column label="学生班级1" align="center" prop="scgStudentClass1" />
      <el-table-column label="学生姓名2" align="center" prop="scgStudentName2" />
      <el-table-column label="学生学号2" align="center" prop="scgStudentId2" />
      <el-table-column label="学生班级2" align="center" prop="scgStudentClass2" />
      <el-table-column label="学生姓名3" align="center" prop="scgStudentName3" />
      <el-table-column label="学生学号3" align="center" prop="scgStudentId3" />
      <el-table-column label="学生班级3" align="center" prop="scgStudentClass3" />
      <el-table-column label="学生姓名4" align="center" prop="scgStudentName4" />
      <el-table-column label="学生学号4" align="center" prop="scgStudentId4" />
      <el-table-column label="学生班级4" align="center" prop="scgStudentClass4" />
      <el-table-column label="学生姓名5" align="center" prop="scgStudentName5" />
      <el-table-column label="学生学号5" align="center" prop="scgStudentId5" />
      <el-table-column label="学生班级5" align="center" prop="scgStudentClass5" />
      <el-table-column label="学生姓名6" align="center" prop="scgStudentName6" />
      <el-table-column label="学生学号6" align="center" prop="scgStudentId6" />
      <el-table-column label="学生班级6" align="center" prop="scgStudentClass6" />
      <el-table-column label="授予部门" align="center" prop="scgAwardingDepartment" />
      <el-table-column label="授予时间" align="center" prop="scgAwardTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scgAwardTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="竞赛类型" align="center" prop="scgCompetitionType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ta_award_level" :value="scope.row.scgCompetitionType"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="未通过原因" align="center" prop="rejectionReason" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="佐证材料" align="center" prop="wdUplocal" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['guidance:guidance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['guidance:guidance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学科竞赛指导对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1040px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :inline="true">
        <el-form-item label="教师姓名" prop="scgTeacherName">
          <el-input v-model="form.scgTeacherName" placeholder="请输入教师姓名" />
        </el-form-item>
        <el-form-item label="教工号" prop="scgEmployeeNumber">
          <el-input v-model="form.scgEmployeeNumber" placeholder="请输入教工号" />
        </el-form-item>
        <el-form-item label="竞赛名称" prop="scgCompetitionName">
          <el-input v-model="form.scgCompetitionName" placeholder="请输入竞赛名称" />
        </el-form-item>
        <el-form-item label="作品名称" prop="scgWinningWorkName">
          <el-input v-model="form.scgWinningWorkName" placeholder="请输入作品名称" />
        </el-form-item>
        <el-form-item label="竞赛级别" prop="scgAwardLevel">
          <el-select v-model="form.scgAwardLevel" placeholder="请选择竞赛级别">
            <el-option
              v-for="dict in dict.type.rp_employee_number"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="获奖等级" prop="scgAwardGrade">
          <el-select v-model="form.scgAwardGrade" placeholder="请选择获奖等级">
            <el-option
              v-for="dict in dict.type.scg_award_grade"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生姓名1" prop="scgStudentName1">
          <el-input v-model="form.scgStudentName1" placeholder="请输入学生姓名1" />
        </el-form-item>
        <el-form-item label="学生学号1" prop="scgStudentId1">
          <el-input v-model="form.scgStudentId1" placeholder="请输入学生学号1" />
        </el-form-item>
        <el-form-item label="学生班级1" prop="scgStudentClass1">
          <el-input v-model="form.scgStudentClass1" placeholder="请输入学生班级1" />
        </el-form-item>
        <el-form-item label="学生姓名2" prop="scgStudentName2">
          <el-input v-model="form.scgStudentName2" placeholder="请输入学生姓名2" />
        </el-form-item>
        <el-form-item label="学生学号2" prop="scgStudentId2">
          <el-input v-model="form.scgStudentId2" placeholder="请输入学生学号2" />
        </el-form-item>
        <el-form-item label="学生班级2" prop="scgStudentClass2">
          <el-input v-model="form.scgStudentClass2" placeholder="请输入学生班级2" />
        </el-form-item>
        <el-form-item label="学生姓名3" prop="scgStudentName3">
          <el-input v-model="form.scgStudentName3" placeholder="请输入学生姓名3" />
        </el-form-item>
        <el-form-item label="学生学号3" prop="scgStudentId3">
          <el-input v-model="form.scgStudentId3" placeholder="请输入学生学号3" />
        </el-form-item>
        <el-form-item label="学生班级3" prop="scgStudentClass3">
          <el-input v-model="form.scgStudentClass3" placeholder="请输入学生班级3" />
        </el-form-item>
        <el-form-item label="学生姓名4" prop="scgStudentName4">
          <el-input v-model="form.scgStudentName4" placeholder="请输入学生姓名4" />
        </el-form-item>
        <el-form-item label="学生学号4" prop="scgStudentId4">
          <el-input v-model="form.scgStudentId4" placeholder="请输入学生学号4" />
        </el-form-item>
        <el-form-item label="学生班级4" prop="scgStudentClass4">
          <el-input v-model="form.scgStudentClass4" placeholder="请输入学生班级4" />
        </el-form-item>
        <el-form-item label="学生姓名5" prop="scgStudentName5">
          <el-input v-model="form.scgStudentName5" placeholder="请输入学生姓名5" />
        </el-form-item>
        <el-form-item label="学生学号5" prop="scgStudentId5">
          <el-input v-model="form.scgStudentId5" placeholder="请输入学生学号5" />
        </el-form-item>
        <el-form-item label="学生班级5" prop="scgStudentClass5">
          <el-input v-model="form.scgStudentClass5" placeholder="请输入学生班级5" />
        </el-form-item>
        <el-form-item label="学生姓名6" prop="scgStudentName6">
          <el-input v-model="form.scgStudentName6" placeholder="请输入学生姓名6" />
        </el-form-item>
        <el-form-item label="学生学号6" prop="scgStudentId6">
          <el-input v-model="form.scgStudentId6" placeholder="请输入学生学号6" />
        </el-form-item>
        <el-form-item label="学生班级6" prop="scgStudentClass6">
          <el-input v-model="form.scgStudentClass6" placeholder="请输入学生班级6" />
        </el-form-item>
        <el-form-item label="授予部门" prop="scgAwardingDepartment">
          <el-input v-model="form.scgAwardingDepartment" placeholder="请输入授予部门" />
        </el-form-item>
        <el-form-item label="授予时间" prop="scgAwardTime">
          <el-date-picker clearable
            v-model="form.scgAwardTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择授予时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="竞赛类型" prop="scgCompetitionType">
          <el-select v-model="form.scgCompetitionType" placeholder="请选择竞赛类型">
            <el-option
              v-for="dict in dict.type.ta_award_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="佐证材料" prop="wdUplocal">
          <file-upload v-model="form.wdUplocal"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGuidance, getGuidance, delGuidance, addGuidance, updateGuidance } from "@/api/guidance/guidance";
import AuditDialog from '@/components/AuditDialog/AuditDialog.vue'; // 引入审核子组件

export default {
  name: "Guidance",
  dicts: ['rp_employee_number', 'ta_award_level', 'scg_award_grade','audit_status'],
  components: { AuditDialog },
  data() {
    return {
      // 控制审核对话框的显示
      auditDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学科竞赛指导表格数据
      guidanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 佐证材料时间范围
      daterangeScgAwardTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scgTeacherName: null,
        scgCompetitionName: null,
        scgWinningWorkName: null,
        scgAwardLevel: null,
        scgAwardGrade: null,
        scgStudentName1: null,
        scgStudentName2: null,
        scgStudentName3: null,
        scgStudentName4: null,
        scgStudentName5: null,
        scgStudentName6: null,
        scgAwardingDepartment: null,
        scgAwardTime: null,
        scgCompetitionType: null,
        auditStatus: null,
        wdUplocal: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 打开审核对话框
    openAuditDialog() {
      if(this.ids!=null)
      {
        this.auditDialogVisible = true; // 显示审核对话框
        this.$refs['audit'].setData(this.ids);
      }
      else
        this.$message.error("请选择要审核的信息");
    },
    // 提交审核数据
    async handleAuditSubmit() {
      this.auditDialogVisible = false; // 关闭审核对话框
      await this.getList(); // 刷新数据列表
    },
    /** 查询学科竞赛指导列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeScgAwardTime && '' != this.daterangeScgAwardTime) {
        this.queryParams.params["beginScgAwardTime"] = this.daterangeScgAwardTime[0];
        this.queryParams.params["endScgAwardTime"] = this.daterangeScgAwardTime[1];
      }
      listGuidance(this.queryParams).then(response => {
        this.guidanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scgId: null,
        scgTeacherName: null,
        scgEmployeeNumber: null,
        scgCompetitionName: null,
        scgWinningWorkName: null,
        scgAwardLevel: null,
        scgAwardGrade: null,
        scgStudentName1: null,
        scgStudentId1: null,
        scgStudentClass1: null,
        scgStudentName2: null,
        scgStudentId2: null,
        scgStudentClass2: null,
        scgStudentName3: null,
        scgStudentId3: null,
        scgStudentClass3: null,
        scgStudentName4: null,
        scgStudentId4: null,
        scgStudentClass4: null,
        scgStudentName5: null,
        scgStudentId5: null,
        scgStudentClass5: null,
        scgStudentName6: null,
        scgStudentId6: null,
        scgStudentClass6: null,
        scgAwardingDepartment: null,
        scgAwardTime: null,
        scgCompetitionType: null,
        auditStatus: null,
        rejectionReason: null,
        userId: null,
        deptId: null,
        updateTime: null,
        wdUplocal: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeScgAwardTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scgId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学科竞赛指导";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scgId = row.scgId || this.ids
      getGuidance(scgId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学科竞赛指导";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.scgId != null) {
            updateGuidance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGuidance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scgIds = row.scgId || this.ids;
      this.$modal.confirm('是否确认删除学科竞赛指导编号为"' + scgIds + '"的数据项？').then(function() {
        return delGuidance(scgIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('guidance/guidance/export', {
        ...this.queryParams
      }, `guidance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
